﻿Imports System.Threading

Imports OPC.Common
Imports OPC.Data.Interface
Imports OPC.Data


Module Main

    Private m_ModName As String = "Main"
    Private saveflag As Integer = 0

    Public Sub Main()
        initSysParam()

        '登录验证
        Dim FrmLogin As New frmLogin
        FrmLogin.ShowDialog()
        FrmLogin.Dispose()
        If FrmLogin.login_state = False Then
            Exit Sub
        End If

        g_frmmain = New frmMain
        Call initOPCServer()
        beginThread()
        g_frmmain.ShowDialog()
        g_frmmain.Dispose()
        sp_Thread.Abort()
        closeOPCConnect()
    End Sub


    '开始线程
    Public Sub beginThread()
        Dim funName As String = "initSerialPort"
        Try
            sp_Thread = New Threading.Thread(AddressOf Port_Cyc)
            sp_Thread.Start()
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub

    Private Sub endThread()
        Dim funName As String = "endThread"
        Try
            sp_Thread.Abort()
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub

    '线程进行轮寻操作
    Private Sub Port_Cyc()
        Dim funname As String = "Port_Cyc"
        Dim flag As Integer = 0
        Dim cmd As String = ""

        '开始轮询进程
        Do
            flag = 0
            Do While (flag < 2)
                time1 = Now
                selectLineData(flag)
                time4 = Now
                'ErrInfoHandle("执行时间", (time2 - time1).TotalSeconds.ToString, (time3 - time2).TotalSeconds.ToString, (time4 - time1).TotalSeconds.ToString)
                'Thread.Sleep(200)
                flag = flag + 1
            Loop
        Loop
    End Sub


    '轮询生产线数据操作
    Public Sub selectLineData(ByVal lineid As Integer)
        Dim funName As String = "selectLineData"
        Dim linedateid(3) As Integer '生产线的数据位置
        Dim i As Integer = 0
        For i = 0 To 3
            linedateid(i) = linenum(lineid, i)
        Next

        Dim arrHSrv(3) As Integer
        Dim arrStat(3) As OPCItemState
        For i = 0 To 3
            arrHSrv(i) = rItm(linedateid(i)).HandleServer
        Next

        Try
            TheGrp.SyncRead(OPCDATASOURCE.OPC_DS_DEVICE, arrHSrv, arrStat)
            '数量变化则进行处理，如果没有变化，则不进行处理
            '总数变化
            If arrStat(0).Quality = 192 Then
                If (arrStat(0).DataValue <> linedata(lineid).producesum) Then
                    linedata(lineid).producesum = arrStat(0).DataValue
                    saveflag = 1
                End If
            Else
                ErrInfoHandle(m_ModName, funName, " 读取arrStat(0)错误", "")
            End If
            '次品数变化
            If arrStat(1).Quality = 192 Then
                If (arrStat(1).DataValue <> linedata(lineid).badpartnum And arrStat(1).DataValue > 0) Then
                    linedata(lineid).badpartnum = arrStat(1).DataValue
                    savePicInfo(lineid + 1, True)
                    badprodnum = badprodnum + 1
                    saveflag = 1
                End If

            Else
                ErrInfoHandle(m_ModName, funName, " 读取arrStat(1)错误", "")
            End If
            '垛数改变
            If arrStat(2).Quality = 192 Then
                If (arrStat(2).DataValue <> linedata(lineid).stackno) Then
                    linedata(lineid).stackno = arrStat(2).DataValue
                    saveflag = 1
                End If
            Else
                ErrInfoHandle(m_ModName, funName, " 读取arrStat(2)错误", "")
            End If
            '包数改变
            If arrStat(3).Quality = 192 Then
                If (Array.IndexOf(Split(stackTopStr(lineid), ","), CStr(arrStat(3).DataValue)) >= 0) Then
                    linedata(lineid).stacknum = arrStat(3).DataValue
                    savePicInfo(lineid + 1, False)
                    saveflag = 1
                Else
                    linedata(lineid).stacknum = arrStat(3).DataValue
                End If
                badprodnum = 0
            Else
                ErrInfoHandle(m_ModName, funName, " 读取arrStat(3)错误", "")
            End If

            'If (saveflag = 1) Then
            '    frmMain.showchange(lineid)
            '    saveflag = 0
            'End If

        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try

    End Sub

    Private Sub initSysParam()

        '更新下最大垛包数
        Dim sqlStr As String = "select belong_lineid ,param_value from t_lineinfo  where param_name='stackTopNum'"
        Dim dt As DataTable = execSQLQuery(sqlStr)
        Dim i As Integer = 0
        For i = 0 To 3
            stackTopStr(i) = "1,2," & CStr(CInt(dt.Rows(i).Item("param_value")) - 1) & "," & CStr(dt.Rows(i).Item("param_value")) & ","
        Next

        sqlStr = "select * from t_sysparam"
        dt = execSQLQuery(sqlStr)
        For i = 0 To dt.Rows.Count - 1
            Select Case dt.Rows(i).Item("param_name").ToString
                Case "classcode"
                    classcode = dt.Rows(i).Item("param_value")
                Case "usercode"
                    username = dt.Rows(i).Item("param_value")
            End Select
        Next

        sqlStr = "select * from t_lineinfo where param_type='onlinecounter' and param_name='stacktopnum' order by belong_lineid"
        dt = execSQLQuery(sqlStr)
        For i = 0 To dt.Rows.Count - 1
            stacktopsum(i) = CInt(dt.Rows(i).Item("param_value"))
        Next
    End Sub


End Module
